<?php
/**
 * Admin_UsersController
 *
 * @author Petr Laslo
 * @license MIT License
 * @package Knihovna\Admin\Controllers
 * @link https://code.google.com/p/knihovna/
 */
 
class Admin_UsersController extends Zend_Controller_Action {

    public function init()
    {

    }

    /**
     * Vypíše všechny uživatele
     *
     * @author Petr Laslo
     * @return void
     */
    public function indexAction()
    {
        $form = new Admin_Form_DeleteUser();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try{
                $post = $this->getRequest()->getPost('id');
                Zend_Debug::dump($post);
                $user = new Knihovna_Model_DbTable_Users();
                $where = $user->getAdapter()->quoteInto('id = ?', $post['id']);
                $user->delete($where);
                $this->_helper->flashMessenger('success|Uživatel byl úspěšně smazán.');
                $this->_redirect('/admin/users/');
            }catch(Zend_Db_Exception $error){
                $this->_helper->flashMessenger('error|'.$error->getMessage());
            }
        }elseif($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))){
            $this->_redirect('/admin/users/');
        }

        $this->view->form = $form;

        $users = new Knihovna_Model_DbTable_Users();
        $select = $users->select()
                        ->order('username');
        $userslist = $users->fetchAll($select);
        $this->view->users = $userslist;

        //TODO paginator

        //vypsání flashMessage
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
        //Zend_Debug::dump($flashMessage->getMessages());
    }

    /**
     * Přidání uživatele
     *
     * @author Petr Laslo
     * @return void
     */
    public function addAction()
    {
        $form = new Admin_Form_AddUser();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $users = new Knihovna_Model_DbTable_Users();
                    $data = array(
                            'username' => $post['user_name'],
                            'passwd' => md5($post['user_password']),
                            'role' => $post['user_role'],
                        );
                    $users->insert($data);

                    $this->_helper->flashMessenger('success|Uživatel "'.$post['user_name'].'" byl úspěšně vytvořen.');
                    $this->_redirect('/admin/users/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                }
            }
        }

        $this->view->form = $form;
    }

    /**
     * Smazání uživatele
     *
     * @author Petr Laslo
     * @return void
     */
    public function deleteAction()
    {
        $form = new Admin_Form_DeleteUser();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try{
                $post = $form->getValues();
                $user = new Knihovna_Model_DbTable_Users();
                $where = $user->getAdapter()->quoteInto('id = ?', $this->_getParam('id'));
                $user->delete($where);
                $this->_helper->flashMessenger('success|Uživatel byl úspěšně smazán.');
                $this->_redirect('/admin/users/');
            }catch(Zend_Db_Exception $error){
                $this->_helper->flashMessenger('error|'.$error->getMessage());
            }
        }elseif($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))){
            $this->_redirect('/admin/users/');
        }

        $this->view->form = $form;
    }

    /**
     * Editace uživatele
     *
     * @author Petr Laslo
     * @return void
     */
    public function editAction()
    {
        //Rozšíření formuláře pro editaci
        $form = new Admin_Form_AddUser();
        $editPass = new Zend_Form_Element_Checkbox('edit_pass',array('label'=>'Změnit heslo?'));
        $form->addElement($editPass);
        $submit = $form->getElement('submit_add');
        $submit->setLabel('Uložit');
        $name = $form->getElement('user_name');
        $name->removeValidator('UserNameUnique');

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('submit_add', false))) {
            if ($form->isValid($this->getRequest()->getPost())) {
                try{
                    $post = $form->getValues();

                    $user = new Knihovna_Model_DbTable_Users();
                    $data = array(
                        'username' => $post['user_name'],
                        'role' => $post['user_role'],
                    );
                    if($post['edit_pass'])
                    {
                        $data['passwd'] = md5($post['user_password']);
                    }

                    $where = $user->getAdapter()->quoteInto('id = ?', $this->_getParam('id'));
                    $user->update($data,$where);
                    $this->_helper->flashMessenger('success|Uživatel byl úspěšně editován.');
                    $this->_redirect('/admin/users/');

                }catch(Exception $error){
                    $this->_helper->flashMessenger('error|'.$error->getMessage());
                }
            }
        }else{
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $user = new Knihovna_Model_DbTable_Users();
                $where = $user->getAdapter()->quoteInto('id = ?', $id);
                $form->populate($user->getUser($where));
            }
        }
        $this->view->form = $form;
    }


}
